import Vue  from "vue";
import Vuex from 'vuex'
import storage from '@/utils/storage'


Vue.use(Vuex)
// 用Vuex.Store对象用来记录token
const store = new Vuex.Store({

    state: {
        // 存储token
        token:"",
        nickname:"" // 可选
    },
    getters: {
        getToken(state){
            return state.token || storage.get("token") || "";
        },
        getNickname(state){
            return state.nickname || storage.get("nickname") || "";
        }
    },
    mutations: {
        // 修改token，并将token存入localStorage
        setToken(state,token,nickname) {
            state.token = token;
            // state.nickname= nickname;
            storage.set('token', token);
            // storage.set('nickname', nickname);
            console.log('store、localstorage保存token成功！');
        },
        delToken(state) {
            state.token = "";
            storage.remove("token");
        },
        delNickname(state) {
            state.nickname= "";
            storage.remove("nickname");
        },
        // 可选
        setNickname(state, nickname) {
            state.nickname = nickname;
            storage.set('nickname', nickname);
            console.log('store、localstorage保存nickname成功！');
        }
    },

    actions: {
        // removeToken: (context) => {
        // context.commit('setToken')
        // }
    },
});

export default store;
